<?php
/*
  +-----------------------------------------------------------------------------------------+
  | Projeto MonitoraSUS                                                                     |
  +-----------------------------------------------------------------------------------------+
  | Copyright (c) 2005-2008 Secretaria Municipal de Saúde de Belo Horizonte / Prodabel      |
  |                                                                                         |
  | Analista Responsável: Hugo Nathaniel Rodrigues <hugonr@gmail.com>, <hugo@pbh.gov.br> |
  |                       Consultor - GSFS - Prodabel / SMSA                                |
  | Departamento Responsável - SMSA: GPLD - Paulinho <pauloces@pbh.gov.br>                  |
  | Departamento Responsável - Prodabel: GSFS - Ricardo Monteiro <monteiro@pbh.gov.br>      |
  | Analista de Informação: Marcone <marcone.costa@pbh.gov.br>                              |
  +-----------------------------------------------------------------------------------------+
 */

//Alterado dia: 26/07/2011 16:19; Por: Mauricio Marques
//Alterado dia: 22/09/2011 11:16; Por: Mauricio Marques (nomes tabelas)
//
// ini_set("default_charset", "ISO-8859-1");
include ("../dados/inc.config.php");
include ("../sistema/inc.convertcharset.php");
$string_conn = "host=$servidorbanco user=$loginbanco password=$senhabanco dbname=$bancodedados";
$conexao = pg_connect($string_conn);
// $conexao = mssql_connect($servidorbanco, $loginbanco, $senhabanco);
// $banco = mssql_select_db($bancodedados, $conexao);
$menu = $_GET["menu"];
$elements = explode("-", $menu);
header("content-type: text/xml");
header("Content-Disposition: attachment; filename=relatorio_acoes.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo "<?xml version=\"1.0\"?>";
echo "<?mso-application progid=\"Excel.Sheet\"?>";
?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
          xmlns:o="urn:schemas-microsoft-com:office:office"
          xmlns:x="urn:schemas-microsoft-com:office:excel"
          xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
          xmlns:html="http://www.w3.org/TR/REC-html40">
    <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
        <Author>Hugo Rodrigues</Author>
        <LastAuthor>Hugo Rodrigues</LastAuthor>
        <Created>2005-08-02T04:06:26Z</Created>
        <LastSaved>2005-08-02T04:30:11Z</LastSaved>
        <Company>Secretaria Municipal de Saúde</Company>
        <Version>11.6360</Version>
    </DocumentProperties>
    <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
        <WindowHeight>8535</WindowHeight>
        <WindowWidth>12345</WindowWidth>
        <WindowTopX>480</WindowTopX>
        <WindowTopY>90</WindowTopY>
        <ProtectStructure>False</ProtectStructure>
        <ProtectWindows>False</ProtectWindows>
    </ExcelWorkbook>
    <Styles>
        <Style ss:ID="Default" ss:Name="Normal">
            <Alignment ss:Vertical="Bottom"/>
            <Borders/>
            <Font/>
            <Interior/>
            <NumberFormat/>
            <Protection/>
        </Style>
        <Style ss:ID="s21" ss:Name="Hyperlink">
            <Font ss:Color="#0000FF" ss:Underline="Single"/>
        </Style>
        <Style ss:ID="s23">
            <Font x:Family="Swiss" ss:Bold="1"/>
        </Style>
        <Style ss:ID="Normal">
            <Font x:Family="Swiss" ss:Bold="0"/>
        </Style>
    </Styles>
    <Worksheet ss:Name="Sheet1">
        <Table x:FullColumns="1">
            <Column ss:Index="1" ss:AutoFitWidth="0" ss:Width="154.5"/>

<?php
if (count($elements) <= 5) {
    $compfimsql = "";
    $espaco = "   ";
    if (count($elements) == 1) {
        $sql = "select * from eixo where num_eixo = " . $elements[0] . " and $where";
        $resultado = pg_query($sql);
        echo "<Row ss:StyleID=\"Normal\">";
        echo "<Cell><Data ss:Type=\"String\">Programa</Data></Cell><Cell><Data ss:Type=\"String\">" . codificar(pg_fetch_result($resultado, 0, "des_eixo")) . "</Data></Cell>";
        echo "</Row>";
        echo "<Row ss:StyleID=\"Normal\">";
        echo "<Cell><Data ss:Type=\"String\">" . codificar(pg_fetch_result($resultado, 0, "informacao_eixo")) . "</Data></Cell>";
        echo "</Row>";
        $sql = "select count(*) as total from objetivos where $where and num_eixo = " . $elements[0];
        $resultado = pg_query($sql);
        echo "<Row ss:StyleID=\"Normal\">";
        //Alterado dia: 26/07/2011 16:19; Por: Mauricio Marques
        //echo "<Cell><Data ss:Type=\"String\">Quantidade de Objetivos Gerais:</Data></Cell><Cell><Data ss:Type=\"String\">" . pg_fetch_result($resultado,0,"total") . "</Data></Cell>";
        echo "<Cell><Data ss:Type=\"String\">Quantidade de Projetos/Atividades:</Data></Cell><Cell><Data ss:Type=\"String\">" . pg_fetch_result($resultado, 0, "total") . "</Data></Cell>";
        echo "</Row>";
        $compfimsql = " and o.num_eixo = " . $elements[0];
    }
    if (count($elements) == 2) {
        $sql = "select * from objetivos where num_objetivo = " . $elements[1] . " and $where";
        $resultado = pg_query($sql);
        echo "<Row ss:StyleID=\"Normal\">";
        echo "<Cell><Data ss:Type=\"String\">Objetivo:</Data></Cell><Cell><Data ss:Type=\"String\">" . codificar(pg_fetch_result($resultado, 0, "des_fant")) . "</Data></Cell>";
        echo "</Row>";
        echo "<Row ss:StyleID=\"Normal\">";
        echo "<Cell><Data ss:Type=\"String\">" . codificar(pg_fetch_result($resultado, 0, "des_objetivo")) . "</Data></Cell>";
        echo "</Row>";
        $sql2 = "select * from orgao where situacao='A' and num_orgao = " . pg_fetch_result($resultado, 0, "num_orgao");
        $resultado2 = pg_query($sql2);
        echo "<Row ss:StyleID=\"Normal\">";
        echo "<Cell><Data ss:Type=\"String\">Orgao Responsável: </Data></Cell><Cell><Data ss:Type=\"String\">" . codificar(pg_fetch_result($resultado2, 0, "des_orgao")) . "</Data></Cell>";
        echo "</Row>";
        $compfimsql = " and d.num_objetivo = " . $elements[1];
    }
    if (count($elements) == 3) {
        $sql = "select * from diretrizes where num_diretriz = " . $elements[2] . " and $where";
        $resultado = pg_query($sql);
        echo "<Row ss:StyleID=\"Normal\">";
        echo "<Cell><Data ss:Type=\"String\">Opera&ccedil;&atilde;o: </Data></Cell><Cell><Data ss:Type=\"String\">" . codificar(pg_fetch_result($resultado, 0, "des_diretriz")) . "</Data></Cell>";
        echo "</Row>";
        if (pg_fetch_result($resultado, 0, "num_func") != "") {
            $sql2 = "select * from funcionario where situacao='A' and num_func = " . pg_fetch_result($resultado, 0, "num_func");
            $resultado2 = pg_query($sql2);
            echo "<Row ss:StyleID=\"Normal\">";
            echo "<Cell><Data ss:Type=\"String\">Funcionário Responsável:  </Data></Cell><Cell><Data ss:Type=\"String\">" . codificar(pg_fetch_result($resultado2, 0, "nom_func")) . "</Data></Cell>";
            echo "</Row>";
        }
        $compfimsql = " and m.num_diretriz = " . $elements[2];
    }
    if (count($elements) == 4) {
        $sql = "select * from objetivos_especificos where num_metas_gestao = " . $elements[3] . " and $where";
        $resultado = pg_query($sql);
        echo "<Row ss:StyleID=\"Normal\">";
        echo "<Cell><Data ss:Type=\"String\">Estrat&eacute;gia:  </Data></Cell><Cell><Data ss:Type=\"String\">" . codificar(pg_fetch_result($resultado, 0, "des_metas_gestao")) . "</Data></Cell>";
        echo "</Row>";
        $compfimsql = " and mt.num_metas_gestao = " . $elements[2];
    }
    if (count($elements) == 5) {
        $sql = "select * from metas where num_meta = " . $elements[4] . " and $where";
        $resultado = pg_query($sql);
        echo "<Row ss:StyleID=\"Normal\">";
        echo "<Cell><Data ss:Type=\"String\">Meta: </Data></Cell><Cell><Data ss:Type=\"String\">" . codificar(pg_fetch_result($resultado, 0, "des_metas_geren")) . "</Data></Cell>";
        echo "</Row>";
        $compfimsql = " and op.num_meta = " . $elements[3];
    }
    if (count($elements) <= 2) {
        if (count($elements) == 1) {
            $sql = "select count(*) as total from objetivos o, diretrizes d where o.exercicio=$exercicio and o.situacao='A' and o.num_objetivo=d.num_objetivo and d.exercicio=$exercicio and d.situacao='A' $compfimsql";
        } else {
            $sql = "select count(*) as total from diretrizes d where d.exercicio=$exercicio and d.situacao='A' $compfimsql";
        }
        $resultado = pg_query($sql);
        echo "<Row ss:StyleID=\"Normal\">";
        echo "<Cell><Data ss:Type=\"String\">Quantidade de Opera&ccedil;&otilde;es: </Data></Cell><Cell><Data ss:Type=\"String\">" . pg_fetch_result($resultado, 0, "total") . "</Data></Cell>";
        echo "</Row>";
    }
    if (count($elements) <= 3) {
        if (count($elements) == 1) {
            $sql = "select count(*) as total from objetivos o, diretrizes d, objetivos_especificos m where o.exercicio=$exercicio and o.situacao='A' and o.num_objetivo=d.num_objetivo and d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and m.num_diretriz=d.num_diretriz $compfimsql";
        } elseif (count($elements) == 2) {
            $sql = "select count(*) as total from diretrizes d, objetivos_especificos m where d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and m.num_diretriz=d.num_diretriz $compfimsql";
        } elseif (count($elements) == 3) {
            $sql = "select count(*) as total from objetivos_especificos m where m.exercicio=$exercicio and m.situacao='A' $compfimsql";
        }
        $resultado = pg_query($sql);
        echo "<Row ss:StyleID=\"Normal\">";
        echo "<Cell><Data ss:Type=\"String\">Quantidade de Estrat&eacute;gias: </Data></Cell><Cell><Data ss:Type=\"String\">" . pg_fetch_result($resultado, 0, "total") . "</Data></Cell>";
        echo "</Row>";
    }
    if (count($elements) <= 4) {
        if (count($elements) == 1) {
            $sql = "select count(*) as total from objetivos o, diretrizes d, objetivos_especificos m, metas mt where o.exercicio=$exercicio and o.situacao='A' and o.num_objetivo=d.num_objetivo and d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao $compfimsql";
        } elseif (count($elements) == 2) {
            $sql = "select count(*) as total from diretrizes d, objetivos_especificos m, metas mt where  d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao $compfimsql";
        } elseif (count($elements) == 3) {
            $sql = "select count(*) as total from objetivos_especificos m, metas mt where  m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and  m.num_metas_gestao=mt.num_metas_gestao $compfimsql";
        } elseif (count($elements) == 4) {
            $sql = "select count(*) as total from metas mt where  mt.exercicio=$exercicio and mt.situacao='A' $compfimsql";
        }
        $resultado = pg_query($sql);
        echo "<Row ss:StyleID=\"Normal\">";
        echo "<Cell><Data ss:Type=\"String\">Quantidade de Metas: </Data></Cell><Cell><Data ss:Type=\"String\">" . pg_fetch_result($resultado, 0, "total") . "</Data></Cell>";
        echo "</Row>";
    }

    if (count($elements) == 1) {
        $sql = "select count(*) as total from objetivos o, diretrizes d, objetivos_especificos m, metas mt, operacao op where o.exercicio=$exercicio and o.situacao='A' and o.num_objetivo=d.num_objetivo and d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_meta=mt.num_meta $compfimsql";
    } elseif (count($elements) == 2) {
        $sql = "select count(*) as total from diretrizes d, objetivos_especificos m, metas mt, operacao op where d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_meta=mt.num_meta $compfimsql";
    } elseif (count($elements) == 3) {
        $sql = "select count(*) as total from objetivos_especificos m, metas mt, operacao op where  m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and m.num_metas_gestao=mt.num_metas_gestao and op.num_meta=mt.num_meta $compfimsql";
    } elseif (count($elements) == 4) {
        $sql = "select count(*) as total from metas mt, operacao op where   mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and  op.num_meta=mt.num_meta $compfimsql";
    } elseif (count($elements) == 5) {
        $sql = "select count(*) as total from operacoes op where op.exercicio=$exercicio and op.situacao='A' $compfimsql";
    }
    $resultado = pg_query($sql);
    echo "<Row ss:StyleID=\"Normal\">";
    echo "<Cell><Data ss:Type=\"String\">Quantidade de Ações: </Data></Cell><Cell><Data ss:Type=\"String\">" . pg_fetch_result($resultado, 0, "total") . "</Data></Cell>";
    echo "</Row>";

    if (count($elements) == 1) {
        $sql = "select count(*) as total from objetivos o, diretrizes d, objetivos_especificos m, metas mt, operacoes op, acoes a where o.exercicio=$exercicio and o.situacao='A' and o.num_objetivo=d.num_objetivo and d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_meta=mt.num_meta and a.num_operacao=op.num_operacao $compfimsql";
    } elseif (count($elements) == 2) {
        $sql = "select count(*) as total from diretrizes d, objetivos_especificos m, metas mt, operacoes op, acoes a where d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_meta=mt.num_meta and a.num_operacao=op.num_operacao $compfimsql";
    } elseif (count($elements) == 3) {
        $sql = "select count(*) as total from objetivos_especificos m, metas mt, operacoes op, acoes a where m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_metas_gestao=mt.num_metas_gestao and op.num_meta=mt.num_meta and a.num_operacao=op.num_operacao $compfimsql";
    } elseif (count($elements) == 4) {
        $sql = "select count(*) as total from metas mt, operacoes op, acoes a where  mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and op.num_meta=mt.num_meta and a.num_operacao=op.num_operacao $compfimsql";
    } elseif (count($elements) == 5) {
        $sql = "select count(*) as total from operacoes op, acoes a where  op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and a.num_operacao=op.num_operacao $compfimsql";
    }
    $resultado = pg_query($sql);
    echo "<Row ss:StyleID=\"Normal\">";
    echo "<Cell><Data ss:Type=\"String\">Quantidade de Atividades: </Data></Cell><Cell><Data ss:Type=\"String\">" . pg_fetch_result($resultado, 0, "total") . "</Data></Cell>";
    echo "</Row>";

    if (count($elements) == 1) {
        $sql = "select count(*) as total from objetivos o, diretrizes d, objetivos_especificos m, metas mt, operacoes op, acoes a where o.exercicio=$exercicio and o.situacao='A' and o.num_objetivo=d.num_objetivo and d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_meta=mt.num_meta and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is not null and ap.situacao='A' and ap.exercicio=$exercicio) $compfimsql";
    } elseif (count($elements) == 2) {
        $sql = "select count(*) as total from diretrizes d, objetivos_especificos m, metas mt, operacoes op, acoes a where d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_meta=mt.num_meta and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is not null and ap.situacao='A' and ap.exercicio=$exercicio) $compfimsql";
    } elseif (count($elements) == 3) {
        $sql = "select count(*) as total from objetivos_especificos m, metas mt, operacoes op, acoes a where m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_metas_gestao=mt.num_metas_gestao and op.num_meta=mt.num_meta and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is not null and ap.situacao='A' and ap.exercicio=$exercicio) $compfimsql";
    } elseif (count($elements) == 4) {
        $sql = "select count(*) as total from  metas mt, operacoes op, acoes a where  mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and op.num_meta=mt.num_meta and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is not null and ap.situacao='A' and ap.exercicio=$exercicio) $compfimsql";
    } elseif (count($elements) == 5) {
        $sql = "select count(*) as total from   operacoes op, acoes a where  op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and  a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is not null and ap.situacao='A' and ap.exercicio=$exercicio) $compfimsql";
    }

    $resultado = pg_query($sql);
    echo "<Row ss:StyleID=\"Normal\">";
    echo "<Cell><Data ss:Type=\"String\">Quantidade de Atividades Fechadas: </Data></Cell><Cell><Data ss:Type=\"String\">" . pg_fetch_result($resultado, 0, "total") . "</Data></Cell>";
    echo "</Row>";

    if (count($elements) == 1) {
        $sql = "select count(*) as total from objetivos o, diretrizes d, objetivos_especificos m, metas mt, operacoes op, acoes a where o.exercicio=$exercicio and o.situacao='A' and o.num_objetivo=d.num_objetivo and d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_meta=mt.num_meta and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is null and ap.situacao='A' and dt_prev_fina<getdate() and dt_real_fina is null and ap.exercicio=$exercicio) $compfimsql";
    } elseif (count($elements) == 2) {
        $sql = "select count(*) as total from  diretrizes d, objetivos_especificos m, metas mt, operacoes op, acoes a where d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_meta=mt.num_meta and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is null and ap.situacao='A' and dt_prev_fina<getdate() and dt_real_fina is null and ap.exercicio=$exercicio) $compfimsql";
    } elseif (count($elements) == 3) {
        $sql = "select count(*) as total from objetivos_especificos m, metas mt, operacoes op, acoes a where m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_metas_gestao=mt.num_metas_gestao and op.num_meta=mt.num_meta and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is null and ap.situacao='A' and dt_prev_fina<getdate() and dt_real_fina is null and ap.exercicio=$exercicio) $compfimsql";
    } elseif (count($elements) == 4) {
        $sql = "select count(*) as total from metas mt, operacoes op, acoes a where  mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and op.num_meta=mt.num_meta and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is null and ap.situacao='A' and dt_prev_fina<getdate() and dt_real_fina is null and ap.exercicio=$exercicio) $compfimsql";
    } elseif (count($elements) == 5) {
        $sql = "select count(*) as total from operacoes op, acoes a where  op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is null and ap.situacao='A' and dt_prev_fina<getdate() and dt_real_fina is null and ap.exercicio=$exercicio) $compfimsql";
    }

    $resultado = pg_query($sql);
    echo "<Row ss:StyleID=\"Normal\">";
    echo "<Cell><Data ss:Type=\"String\">Quantidade de Atividades Atrasadas: </Data></Cell><Cell><Data ss:Type=\"String\">" . pg_fetch_result($resultado, 0, "total") . "</Data></Cell>";
    echo "</Row>";

    if (count($elements) == 1) {
        $sql = "select count(*) as total from objetivos o, diretrizes d, objetivos_especificos m, metas mt, operacoes op, acoes a where o.exercicio=$exercicio and o.situacao='A' and o.num_objetivo=d.num_objetivo and d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_meta=mt.num_meta and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is null and ap.situacao='A' and dt_prev_fina>=getdate() and ap.exercicio=$exercicio) $compfimsql";
    } elseif (count($elements) == 2) {
        $sql = "select count(*) as total from diretrizes d, objetivos_especificos m, metas mt, operacoes op, acoes a where d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_meta=mt.num_meta and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is null and ap.situacao='A' and dt_prev_fina>=getdate() and ap.exercicio=$exercicio) $compfimsql";
    } elseif (count($elements) == 3) {
        $sql = "select count(*) as total from objetivos_especificos m, metas mt, operacoes op, acoes a where m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_metas_gestao=mt.num_metas_gestao and op.num_meta=mt.num_meta and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is null and ap.situacao='A' and dt_prev_fina>=getdate() and ap.exercicio=$exercicio) $compfimsql";
    } elseif (count($elements) == 4) {
        $sql = "select count(*) as total from metas mt, operacoes op, acoes a where  mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and op.num_meta=mt.num_meta and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is null and ap.situacao='A' and dt_prev_fina>=getdate() and ap.exercicio=$exercicio) $compfimsql";
    } elseif (count($elements) == 5) {
        $sql = "select count(*) as total from  operacoes op, acoes a where  op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and a.num_operacao=op.num_operacao and a.num_acao in (select ap.num_acao from acao_prazo ap where ap.dt_real_fina is null and ap.situacao='A' and dt_prev_fina>=getdate() and ap.exercicio=$exercicio) $compfimsql";
    }

    $resultado = pg_query($sql);
    echo "<Row ss:StyleID=\"Normal\">";
    echo "<Cell><Data ss:Type=\"String\">Quantidade de Atividades no prazo: </Data></Cell><Cell><Data ss:Type=\"String\">" . pg_fetch_result($resultado, 0, "total") . "</Data></Cell>";
    echo "</Row>";

    if (count($elements) == 1) {
        $sql = "select count(*) as total from objetivos o, diretrizes d, objetivos_especificos m, metas mt, operacoes op, acoes a where o.exercicio=$exercicio and o.situacao='A' and o.num_objetivo=d.num_objetivo and d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_meta=mt.num_meta and a.num_operacao=op.num_operacao and a.num_acao not in (select ap.num_acao from acao_prazo ap where ap.situacao='A' and ap.exercicio=$exercicio) $compfimsql";
    } elseif (count($elements) == 2) {
        $sql = "select count(*) as total from diretrizes d, objetivos_especificos m, metas mt, operacoes op, acoes a where d.exercicio=$exercicio and d.situacao='A' and m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_diretriz=d.num_diretriz and m.num_metas_gestao=mt.num_metas_gestao and op.num_meta=mt.num_meta and a.num_operacao=op.num_operacao and a.num_acao not in (select ap.num_acao from acao_prazo ap where ap.situacao='A' and ap.exercicio=$exercicio) $compfimsql";
    } elseif (count($elements) == 3) {
        $sql = "select count(*) as total from objetivos_especificos m, metas mt, operacoes op, acoes a where m.exercicio=$exercicio and m.situacao='A' and mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and m.num_metas_gestao=mt.num_metas_gestao and op.num_meta=mt.num_meta and a.num_operacao=op.num_operacao and a.num_acao not in (select ap.num_acao from acao_prazo ap where ap.situacao='A' and ap.exercicio=$exercicio) $compfimsql";
    } elseif (count($elements) == 4) {
        $sql = "select count(*) as total from  metas mt, operacoes op, acoes a where  mt.exercicio=$exercicio and mt.situacao='A' and op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and  op.num_meta=mt.num_meta and a.num_operacao=op.num_operacao and a.num_acao not in (select ap.num_acao from acao_prazo ap where ap.situacao='A' and ap.exercicio=$exercicio) $compfimsql";
    } elseif (count($elements) == 5) {
        $sql = "select count(*) as total from operacoes op, acoes a where op.exercicio=$exercicio and op.situacao='A' and a.exercicio=$exercicio and a.situacao='A' and a.num_operacao=op.num_operacao and a.num_acao not in (select ap.num_acao from acao_prazo ap where ap.situacao='A' and ap.exercicio=$exercicio) $compfimsql";
    }
    ?>
                <Row ss:StyleID="s23">
                </Row>
                <Row ss:StyleID="s23">
                    <Cell><Data ss:Type="String">Ação</Data></Cell>
                    <Cell><Data ss:Type="String">Vencimento</Data></Cell>
                </Row>

    <?php
    $resultado = pg_query($sql);
    echo "<Row ss:StyleID=\"Normal\">";
    echo "<Cell><Data ss:Type=\"String\">Quantidade de Atividades sem prazo definido: </Data></Cell><Cell><Data ss:Type=\"String\">" . pg_fetch_result($resultado, 0, "total") . "</Data></Cell>";
    echo "</Row>";
    echo "<Row ss:StyleID=\"Normal\">";
    echo "<Cell><Data ss:Type=\"String\">Texto em  negrito  indica Atividade em atraso\nTexto em  itálico  indica Atividade fechada\nTexto sem formataçao indica Atividade aberta mas no prazo.</Data></Cell>";
    echo "</Row>";
    if (count($elements) == 1) { // eixo
        $sql = "select * from eixo where Num_Eixo = " . $elements[0] . " and $where";
        $resultado = pg_query($sql);
        echo "<Row ss:StyleID=\"Normal\">";
        echo "<Cell><Data ss:Type=\"String\">Programa: </Data></Cell><Cell><Data ss:Type=\"String\">" . codificar(pg_fetch_result($resultado, 0, "des_eixo")) . "</Data></Cell>";
        echo "</Row>";
        $num_eixo = pg_fetch_result($resultado, 0, "num_eixo");
        $sql = "select * from objetivos where num_eixo = " . $num_eixo . " and $where";
        $resultado = pg_query($sql);
        while ($rs = pg_fetch_array($resultado)) {
            echo "<Row ss:StyleID=\"Normal\">";
            echo "<Cell><Data ss:Type=\"String\">Projeto/Atividade </Data></Cell><Cell><Data ss:Type=\"String\">" . $rs["num_objetivo"] . ": ";
            echo codificar($rs["des_objetivo"]) . "</Data></Cell>";
            echo "</Row>";
            $num_objetivo = $rs["num_objetivo"];
            $sql2 = "select * from diretrizes where $where and num_objetivo = $num_objetivo";
            $resultado2 = pg_query($sql2);
            while ($rs2 = pg_fetch_array($resultado2)) {
                echo "<Row ss:StyleID=\"Normal\">";
                echo "<Cell><Data ss:Type=\"String\">Opera&ccedil;&atilde;o </Data></Cell><Cell><Data ss:Type=\"String\">" . $rs2["num_diretriz"] . ": ";
                echo codificar($rs2["des_diretriz"]) . "</Data></Cell>";
                echo "</Row>";
                $num_diretriz = $rs2["num_diretriz"];
                $sql3 = "select * from objetivos_especificos where $where and num_diretriz = $num_diretriz";
                $resultado3 = pg_query($sql3);
                while ($rs3 = pg_fetch_array($resultado3)) {
                    echo "<Row ss:StyleID=\"Normal\">";
                    echo "<Cell><Data ss:Type=\"String\">Estrat&eacute;gia </Data></Cell><Cell><Data ss:Type=\"String\">" . $rs3["num_metas_gestao"] . ": ";
                    echo codificar($rs3["des_metas_gestao"]) . "</Data></Cell>";
                    echo "</Row>";
                    $num_metas_gestao = $rs3["num_metas_gestao"];
                    $sql4 = "select * from metas where $where and num_metas_gestao = $num_metas_gestao";
                    $resultado4 = pg_query($sql4);
                    while ($rs4 = pg_fetch_array($resultado4)) {
                        echo "<Row ss:StyleID=\"Normal\">";
                        echo "<Cell><Data ss:Type=\"String\">Meta " . $rs4["num_metas_geren"] . ": </Data></Cell><Cell><Data ss:Type=\"String\">";
                        echo codificar($rs4["des_metas_geren"]) . "</Data></Cell>";
                        echo "</Row>";
                        $num_metas_geren = $rs4["num_meta"];
                        $sql5 = "select d.des_descritivo as Des_Operacao, o.* from operacoes o, descritivo d where $where and o.num_descritivo = d.num_descritivo and num_meta = $num_metas_geren";
                        $resultado5 = pg_query($sql5);
                        while ($rs5 = pg_fetch_array($resultado5)) {
                            echo "<Row ss:StyleID=\"Normal\">";
                            echo "<Cell><Data ss:Type=\"String\">Açao " . $rs5["num_operacao"] . ": </Data></Cell><Cell><Data ss:Type=\"String\">";
                            echo codificar($rs5["des_operacao"]) . "</Data></Cell>";
                            echo "</Row>";
                            $num_operacao = $rs5["num_operacao"];
                            $sql6 = "select d.des_descritivo as Des_Acao, a.* from acoes a, descritivos d where $where and a.num_descritivo = d.num_descritivo and num_operacao = $num_operacao";
                            $resultado6 = pg_query($sql6);
                            while ($rs6 = pg_fetch_array($resultado6)) {
                                $num_acao = $rs6["num_acao"];
                                $sql7 = "select count(*) as total from acao_prazos where $where and dt_real_fina is not null and num_acao=$num_acao";
                                $resultado7 = pg_query($sql7);
                                $sql8 = "select count(*) as total from acao_prazos where $where and dt_prev_fina<getdate() and dt_real_fina is null and num_acao=$num_acao";
                                $resultado8 = pg_query($sql8);
                                $sql9 = "select case when dt_real_fina is not null then to_char(dt_real_fina,'dd/mm/yyyy') else to_char(dt_prev_fina,'dd/mm/yyyy') end as data from acao_prazo where $where and num_acao = $num_acao";
                                $resultado9 = pg_query($sql9);
                                echo "<Row ss:StyleID=\"Normal\">";

                                echo "<Cell><Data ss:Type=\"String\">Atividade " . $rs6["num_acao"] . ": ";
                                echo codificar($rs6["des_acao"]) . " </Data></Cell><Cell><Data ss:Type=\"String\">" . trim(pg_fetch_result($resultado9, 0, "data")) . "</Data></Cell>";
                                echo "</Row>";
                            }
                        }
                    }
                }
            }
        }
    } // fim eixo

    if (count($elements) == 2) { // objetivo geral
        $sql = "select * from objetivos where num_objetivo = " . $elements[1] . " and $where";
        $resultado = pg_query($sql);
        echo "<Row ss:StyleID=\"Normal\">";
        echo "<Cell><Data ss:Type=\"String\">Projeto/Atividade " . pg_fetch_result($resultado, 0, "num_objetivo") . ": </Data></Cell><Cell><Data ss:Type=\"String\">";
        echo codificar(pg_fetch_result($resultado, 0, "des_objetivo")) . "</Data></Cell>";
        echo "</Row>";
        $num_objetivo = pg_fetch_result($resultado, 0, "num_objetivo");
        $sql2 = "select * from diretrizes where $where and num_objetivo = $num_objetivo";
        $resultado2 = pg_query($sql2);
        while ($rs2 = pg_fetch_array($resultado2)) {
            echo "<Row ss:StyleID=\"Normal\">";
            echo "<Cell><Data ss:Type=\"String\">Opera&ccedil;&atilde;o " . $rs2["num_diretriz"] . ": </Data></Cell><Cell><Data ss:Type=\"String\">";
            echo codificar($rs2["des_diretriz"]) . "</Data></Cell>";
            echo "</Row>";
            $num_diretriz = $rs2["num_diretriz"];
            $sql3 = "select * from objetivos_especificos where $where and num_diretriz = $num_diretriz";
            $resultado3 = pg_query($sql3);
            while ($rs3 = pg_fetch_array($resultado3)) {
                echo "<Row ss:StyleID=\"Normal\">";
                echo "<Cell><Data ss:Type=\"String\">Estratégia " . $rs3["num_metas_gestao"] . ": </Data></Cell><Cell><Data ss:Type=\"String\">";
                echo codificar($rs3["des_metas_gestao"]) . "</Data></Cell>";
                echo "</Row>";
                $num_metas_gestao = $rs3["num_metas_gestao"];
                $sql4 = "select * from metas where $where and num_metas_gestao = $num_metas_gestao";
                $resultado4 = pg_query($sql4);
                while ($rs4 = pg_fetch_array($resultado4)) {
                    echo "<Row ss:StyleID=\"Normal\">";
                    echo "<Cell><Data ss:Type=\"String\">Meta " . $rs4["num_meta"] . ": </Data></Cell><Cell><Data ss:Type=\"String\">";
                    echo codificar($rs4["des_metas_geren"]) . "</Data></Cell>";
                    echo "</Row>";
                    $num_metas_geren = $rs4["num_meta"];
                    $sql5 = "select d.des_descritivo as Des_Operacao, o.* from operacoes o, descritivos d where $where and o.num_descritivo = d.num_descritivo and num_meta = $num_metas_geren";
                    $resultado5 = pg_query($sql5);
                    while ($rs5 = pg_fetch_array($resultado5)) {
                        echo "<Row ss:StyleID=\"Normal\">";
                        echo "<Cell><Data ss:Type=\"String\">Ação " . $rs5["num_operacao"] . ": </Data></Cell><Cell><Data ss:Type=\"String\">";
                        echo codificar($rs5["des_operacao"]) . "</Data></Cell>";
                        echo "</Row>";
                        $num_operacao = $rs5["num_operacao"];
                        $sql6 = "select d.des_descritivo as Des_Acao, a.* from acao a, descritivos d where $where and a.num_descritivo = d.num_descritivo and num_operacao = $num_operacao";
                        $resultado6 = pg_query($sql6);
                        while ($rs6 = pg_fetch_array($resultado6)) {
                            $num_acao = $rs6["num_acao"];
                            $sql7 = "select count(*) as total from acao_prazos where $where and dt_real_fina is not null and num_acao=$num_acao";
                            $resultado7 = pg_query($sql7);
                            $sql8 = "select count(*) as total from acao_prazos where $where and dt_prev_fina<getdate() and dt_real_fina is null and num_acao=$num_acao";
                            $resultado8 = pg_query($sql8);
                            $sql9 = "select case when dt_real_fina is not null then to_char(dt_real_fina,'dd/mm/yyyy') else to_char(dt_prev_fina,'dd/mm/yyyy') end as data from acao_prazo where $where and num_acao = $num_acao";
                            $resultado9 = pg_query($sql9);
                            echo "<Row ss:StyleID=\"Normal\">";
                            echo "<Cell><Data ss:Type=\"String\">Atividade " . $rs6["num_acao"] . ": ";
                            echo codificar($rs6["des_acao"]) . " </Data></Cell><Cell><Data ss:Type=\"String\">" . trim(pg_fetch_result($resultado9, 0, "data")) . "</Data></Cell>";
                            echo "</Row>";
                        }
                    }
                }
            }
        }
    } // fim objetivo

    if (count($elements) == 3) { // diretriz
        $sql2 = "select * from diretriz where $where and num_diretriz = " . $elements[2];
        $resultado2 = pg_query($sql2);
        while ($rs2 = pg_fetch_array($resultado2)) {
            echo "<Row ss:StyleID=\"Normal\">";
            echo "<Cell><Data ss:Type=\"String\">Operação " . $rs2["num_diretriz"] . ": </Data></Cell><Cell><Data ss:Type=\"String\">";
            echo codificar($rs2["des_diretriz"]) . "</Data></Cell>";
            echo "</Row>";
            $num_diretriz = $rs2["num_diretriz"];
            $sql3 = "select * from objetivos_especificos where $where and num_diretriz = $num_diretriz";
            $resultado3 = pg_query($sql3);
            while ($rs3 = pg_fetch_array($resultado3)) {
                echo "<Row ss:StyleID=\"Normal\">";
                echo "<Cell><Data ss:Type=\"String\">Estratégia " . $rs3["num_metas_gestao"] . ": </Data></Cell><Cell><Data ss:Type=\"String\">";
                echo codificar($rs3["des_metas_gestao"]) . "</Data></Cell>";
                echo "</Row>";
                $num_metas_gestao = $rs3["num_metas_gestao"];
                $sql4 = "select * from metas where $where and num_metas_gestao = $num_metas_gestao";
                $resultado4 = pg_query($sql4);
                while ($rs4 = pg_fetch_array($resultado4)) {
                    echo "<Row ss:StyleID=\"Normal\">";
                    echo "<Cell><Data ss:Type=\"String\">Meta " . $rs4["num_meta"] . ": </Data></Cell><Cell><Data ss:Type=\"String\">";
                    echo codificar($rs4["des_metas_geren"]) . "</Data></Cell>";
                    echo "</Row>";
                    $num_metas_geren = $rs4["num_meta"];
                    $sql5 = "select d.des_descritivo as Des_Operacao, o.* from operacoes o, descritivos d where $where and o.num_descritivo = d.num_descritivo and num_meta = $num_metas_geren";
                    $resultado5 = pg_query($sql5);
                    while ($rs5 = pg_fetch_array($resultado5)) {
                        echo "<Row ss:StyleID=\"Normal\">";
                        echo "<Cell><Data ss:Type=\"String\">Ação " . $rs5["num_operacao"] . ": </Data></Cell><Cell><Data ss:Type=\"String\">";
                        echo codificar($rs5["des_operacao"]) . "</Data></Cell>";
                        echo "</Row>";
                        $num_operacao = $rs5["num_operacao"];
                        $sql6 = "select d.des_descritivo as Des_Acao, a.* from acoes a, descritivos d where $where and a.num_descritivo = d.num_descritivo and num_operacao = $num_operacao";
                        $resultado6 = pg_query($sql6);
                        while ($rs6 = pg_fetch_array($resultado6)) {
                            $num_acao = $rs6["num_acao"];
                            $sql7 = "select count(*) as total from acao_prazos where $where and dt_real_fina is not null and num_acao=$num_acao";
                            $resultado7 = pg_query($sql7);
                            $sql8 = "select count(*) as total from acao_prazos where $where and dt_prev_fina<getdate() and dt_real_fina is null and num_acao=$num_acao";
                            $resultado8 = pg_query($sql8);
                            $sql9 = "select case when dt_real_fina is not null then to_char(dt_real_fina,'dd/mm/yyyy') else to_char(dt_prev_fina,'dd/mm/yyyy') end as data from acao_prazo where $where and Num_Acao = $num_acao";
                            $resultado9 = pg_query($sql9);
                            echo "<Row ss:StyleID=\"Normal\">";
                            echo "<Cell><Data ss:Type=\"String\">Atividade " . $rs6["num_acao"] . ": ";
                            echo codificar($rs6["des_acao"]) . " </Data></Cell><Cell><Data ss:Type=\"String\">" . trim(pg_fetch_result($resultado9, 0, "data")) . "</Data></Cell>";
                            echo "</Row>";
                        }
                    }
                }
            }
        }
    } // fim diretriz

    if (count($elements) == 4) { // objetivo especifico
        $sql3 = "select * from objetivos_especificos where $where and num_metas_gestao = " . $elements[3];
        $resultado3 = pg_query($sql3);
        while ($rs3 = pg_fetch_array($resultado3)) {
            echo "<Row ss:StyleID=\"Normal\">";
            echo "<Cell><Data ss:Type=\"String\">Estratégia " . $rs3["num_metas_gestao"] . ": </Data></Cell><Cell><Data ss:Type=\"String\">";
            echo codificar($rs3["des_metas_gestao"]) . "</Data></Cell>";
            echo "</Row>";
            $num_metas_gestao = $rs3["num_metas_gestao"];
            $sql4 = "select * from metas where $where and num_metas_gestao = $num_metas_gestao";
            $resultado4 = pg_query($sql4);
            while ($rs4 = pg_fetch_array($resultado4)) {
                echo "<Row ss:StyleID=\"Normal\">";
                echo "<Cell><Data ss:Type=\"String\">Meta " . $rs4["num_meta"] . ": </Data></Cell><Cell><Data ss:Type=\"String\">";
                echo codificar($rs4["des_metas_geren"]) . "</Data></Cell>";
                echo "</Row>";
                $num_metas_geren = $rs4["num_meta"];
                $sql5 = "select d.des_descritivo as Des_Operacao, o.* from operacoes o, descritivos d where $where and o.num_descritivo = d.num_descritivo and num_meta = $num_metas_geren";
                $resultado5 = pg_query($sql5);
                while ($rs5 = pg_fetch_array($resultado5)) {
                    echo "<Row ss:StyleID=\"Normal\">";
                    echo "<Cell><Data ss:Type=\"String\">Ação " . $rs5["num_operacao"] . ": </Data></Cell><Cell><Data ss:Type=\"String\">";
                    echo codificar($rs5["des_operacao"]) . "</Data></Cell>";
                    echo "</Row>";
                    $num_operacao = $rs5["num_operacao"];
                    $sql6 = "select d.des_descritivo as Des_Acao, a.* from acoes a, descritivos d where $where and a.num_descritivo = d.num_descritivo and num_operacao = $num_operacao";
                    $resultado6 = pg_query($sql6);
                    while ($rs6 = pg_fetch_array($resultado6)) {
                        $num_acao = $rs6["num_acao"];
                        $sql7 = "select count(*) as total from acao_prazos where $where and dt_real_fina is not null and num_acao=$num_acao";
                        $resultado7 = pg_query($sql7);
                        $sql8 = "select count(*) as total from acao_prazos where $where and dt_prev_fina<getdate() and dt_real_fina is null and num_acao=$num_acao";
                        $resultado8 = pg_query($sql8);
                        $sql9 = "select case when dt_real_fina is not null then to_char(dt_real_fina,'dd/mm/yyyy') else to_char(dt_prev_fina,'dd/mm/yyyy') end as data from acao_prazo where $where and num_acao = $num_acao";
                        $resultado9 = pg_query($sql9);
                        echo "<Row ss:StyleID=\"Normal\">";
                        echo "<Cell><Data ss:Type=\"String\">Atividade " . $rs6["num_acao"] . ": ";
                        echo codificar($rs6["des_acao"]) . " </Data></Cell><Cell><Data ss:Type=\"String\">" . trim(pg_fetch_result($resultado9, 0, "data")) . "</Data></Cell>";
                        echo "</Row>";
                    }
                }
            }
        }
    } // fim objetivo especifico

    if (count($elements) == 5) { // metas
        $sql4 = "select * from metas where $where and num_meta = " . $elements[4];
        $resultado4 = pg_query($sql4);
        while ($rs4 = pg_fetch_array($resultado4)) {
            echo "<Row ss:StyleID=\"Normal\">";
            echo "<Cell><Data ss:Type=\"String\">Meta " . $rs4["num_meta"] . ": </Data></Cell><Cell><Data ss:Type=\"String\">";
            echo codificar($rs4["des_metas_geren"]) . "</Data></Cell>";
            echo "</Row>";
            $num_metas_geren = $rs4["num_meta"];
            $sql5 = "select d.des_descritivo as des_operacao, o.* from operacoes o, descritivos d where $where and o.num_descritivo = d.num_descritivo and num_meta = $num_metas_geren";
            $resultado5 = pg_query($sql5);
            while ($rs5 = pg_fetch_array($resultado5)) {
                echo "<Row ss:StyleID=\"Normal\">";
                echo "<Cell><Data ss:Type=\"String\">Ação " . $rs5["num_operacao"] . ": </Data></Cell><Cell><Data ss:Type=\"String\">";
                echo codificar($rs5["des_operacao"]) . "</Data></Cell>";
                echo "</Row>";
                $num_operacao = $rs5["num_operacao"];
                $sql6 = "select d.des_descritivo as Des_Acao, a.* from acoes a, descritivos d where $where and a.num_descritivo = d.num_descritivo and num_operacao = $num_operacao";
                $resultado6 = pg_query($sql6);
                while ($rs6 = pg_fetch_array($resultado6)) {
                    $num_acao = $rs6["num_acao"];
                    $sql7 = "select count(*) as total from acao_prazos where $where and dt_real_fina is not null and num_acao=$num_acao";
                    $resultado7 = pg_query($sql7);
                    $sql8 = "select count(*) as total from acao_prazos where $where and dt_prev_fina<getdate() and dt_real_fina is null and num_acao=$num_acao";
                    $resultado8 = pg_query($sql8);

                    $sql9 = "select case when dt_real_fina is not null then to_char(dt_real_fina,'dd/mm/yyyy') else to_char(dt_prev_fina,'dd/mm/yyyy') end as data from acao_prazo where $where and num_acao = $num_acao";
                    $resultado9 = pg_query($sql9);
                    echo "<Row ss:StyleID=\"Normal\">";
                    $des_acao = "<Cell><Data ss:Type=\"String\">Atividade " . $rs6["num_acao"] . ": " . codificar(str_replace("\n", "", trim($rs6["des_acao"])));
                    echo $des_acao . " </Data></Cell><Cell><Data ss:Type=\"DateTime\">";
                    $temporario = pg_fetch_result($resultado9, 0, "data");
                    $valordata = explode("/", $temporario);
                    echo $valordata[2] . "-" . $valordata[1] . "-" . $valordata[0] . "T12:30:00.000";
                    // echo trim(pg_fetch_result($resultado9,0,"data")) . 
                    echo "</Data></Cell>";
                    echo "</Row>";
                }
            }
        }
    } // fim metas
}
?>






        </Table>
        <WorksheetOptions 
            xmlns="urn:schemas-microsoft-com:office:excel">
            <Print>
                <ValidPrinterInfo/>
                <HorizontalResolution>300</HorizontalResolution>
                <VerticalResolution>300</VerticalResolution>
            </Print>
            <Selected/>
            <Panes>
                <Pane>
                    <Number>3</Number>
                    <ActiveRow>5</ActiveRow>
                </Pane>
            </Panes>
            <ProtectObjects>False</ProtectObjects>
            <ProtectScenarios>False</ProtectScenarios>
        </WorksheetOptions>
    </Worksheet>
    <Worksheet ss:Name="Sheet2">
        <WorksheetOptions 
            xmlns="urn:schemas-microsoft-com:office:excel">
            <ProtectObjects>False</ProtectObjects>
            <ProtectScenarios>False</ProtectScenarios>
        </WorksheetOptions>
    </Worksheet>
    <Worksheet ss:Name="Sheet3">
        <WorksheetOptions 
            xmlns="urn:schemas-microsoft-com:office:excel">
            <ProtectObjects>False</ProtectObjects>
            <ProtectScenarios>False</ProtectScenarios>
        </WorksheetOptions>
    </Worksheet>
</Workbook>

